package cn.huhst.cas.weisiqian.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import cn.huhst.cas.weisiqian.dbhelp.DBHelper;
import cn.huhst.cas.weisiqian.pojo.PostType;

public class PostDao {

	private Logger logger = Logger.getLogger(PostDao.class);

	public boolean addPostType(String posttype) throws DuplicateEntryException {
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		String sql = "INSERT INTO posttype value(?)";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, posttype);
			int count = pstmt.executeUpdate();
			if (count > 0) {
				logger.info("添加帖子分类" + posttype + "成功");
				return true;
			} else {
				logger.info("添加帖子分类" + posttype + "失败");
				return false;
			}
		} catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e) {
			logger.error(posttype + "帖子分类已存在", e);
			throw new DuplicateEntryException(posttype + "帖子分类已存在");
		} catch (SQLException e) {
			logger.error("添加帖子分类出错");
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, null);
			logger.info("关闭数据库连接");
		}
		return false;
	}

	public int getPostTypeCount() {
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT COUNT(*) FROM posttype";
		int count = 0;
		try {
			pstmt = connection.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
				logger.info("帖子分类数目为：" + count);
			}
		} catch (SQLException e) {
			logger.error("查询帖子分类数目出错", e);
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, rs);
			logger.info("关闭数据库连接");
		}

		return count;
	}

	public ArrayList<PostType> getPostType(int recorderIndex, int pageSize) {
		ArrayList<PostType> postTypes = new ArrayList<PostType>();
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM posttype LIMIT ?,?";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, recorderIndex);
			pstmt.setInt(2, pageSize);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				PostType posttype = new PostType(rs.getString("name"));
				postTypes.add(posttype);
				logger.debug(posttype);
			}
		} catch (SQLException e) {
			logger.error("分页查询帖子分类数目出错");
		} finally {
			DBHelper.closeDBHelp(connection, pstmt, rs);
			logger.info("关闭数据库连接");
		}
		return postTypes;
	}

	public int deletePostType(String posttype) {
		int count = 0;
		Connection connection = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		String sql = "DELETE FROM posttype WHERE name=?";
		logger.info("sql:" + sql);
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, posttype);
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error("删除帖子分类：" + posttype + "失败");
		} finally{
			DBHelper.closeDBHelp(connection, pstmt, null);
			logger.info("关闭数据库连接");
		}
		return count;
	}

}
